package com.my.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.beust.jcommander.internal.Maps;
import com.google.common.collect.Lists;
import com.my.biz.service.ISysConfigService;

@Component
public class MailUtil {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class);
	
	@Autowired
	private ISysConfigService configService;
	

	public Boolean sendRegCodeQQEMail(String to,String code){
		Map<String, Object> param = Maps.newHashMap();
		param.put("code", code);
		param.put("mail", to);
		return sendQQEMail(Lists.newArrayList(to), "请激活你的群管理平台帐号", param, "reg_mail.ftl");
	}
	
	public Boolean sendQQEMail(List<String> to,String subject,Map<String, Object> data,String template){
		 try{
			  HtmlEmail htmlEmail = new HtmlEmail();
			  htmlEmail.setHostName(configService.getStringValueByKey("qq_mail_host"));
			  htmlEmail.setSmtpPort(configService.getIntegerValueByKey("qq_mail_port"));
			  htmlEmail.setSSLOnConnect(true);
			  htmlEmail.setAuthenticator(new DefaultAuthenticator(configService.getStringValueByKey("qq_mail_user_name"), configService.getStringValueByKey("qq_mail_user_password")));
			  for (String u : to) {
				  htmlEmail.addTo(u);
			  }
			  htmlEmail.setFrom(configService.getStringValueByKey("qq_mail_from_mail_name"),configService.getStringValueByKey("qq_mail_from_name"));
			  htmlEmail.setSubject(subject);
			  htmlEmail.setCharset(configService.getStringValueByKey("qq_mail_charset")); 
			  htmlEmail.setHtmlMsg(FreemarkUtils.getValueByTemplate(data, template));
			  htmlEmail.send();
			  return Boolean.TRUE;
		 }catch(Exception e){
			 LOGGER.error("send QQ EMail exception to={},subject={},data={},template={},exception={}",new Object[]{to,subject,data,template,e});
		 }
		 return Boolean.FALSE;
	}
	
	public static void main(String[] args) throws Exception {
		/*Email email = new SimpleEmail();
		email.setHostName("smtp.qq.com");
		email.setSmtpPort(465);
		email.setAuthenticator(new DefaultAuthenticator("17580142", "ywyygofsfhoebifd"));
		email.setSSLOnConnect(true);
		email.setFrom("17580142@qq.com");
		email.setSubject("TestMail");
		email.setMsg("This is a test mail ... :-)");
		email.addTo("673976475@qq.com");
		email.send();*/
		
		  HtmlEmail htmlEmail = new HtmlEmail();
		  htmlEmail.setHostName("smtp.qq.com");
		  htmlEmail.setSmtpPort(465);
		  htmlEmail.setSSLOnConnect(true);
		  htmlEmail.setAuthenticator(new DefaultAuthenticator("17580142", "ywyygofsfhoebifd"));
		  htmlEmail.addTo("673976475@qq.com");
		  htmlEmail.setFrom("17580142@qq.com","q群管理平台");
		  htmlEmail.setSubject("请激活你的群管理平台帐号");
		  htmlEmail.setCharset("gbk");  
		 
		  // set the html message
		  htmlEmail.setHtmlMsg(FreemarkUtils.getValueByTemplate(Maps.<String, Object>newHashMap(), "reg_mail.ftl"));
		  

		  // set the alternative message
		  //htmlEmail.setTextMsg("Your email client does not support HTML messages");

		  // send the email
		  htmlEmail.send();
	}
}
